package cn.openea.perpartner.componet.web.service.impl;

import cn.hutool.extra.cglib.CglibUtil;
import cn.openea.perpartner.common.core.manager.AsyncManager;
import cn.openea.perpartner.componet.web.entity.HisShareLog;
import cn.openea.perpartner.componet.web.entity.InsShareLog;
import cn.openea.perpartner.componet.web.mapper.HisShareLogMapper;
import cn.openea.perpartner.componet.web.service.IHisShareLogService;
import cn.openea.perpartner.componet.web.service.InsShareLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.TimerTask;

/**
 * 历史分享记录(ShareHisLog)表服务实现类
 *
 * @author Weird077
 * @since 2023-03-01 15:40:13
 */
@Service
public class HisShareLogServiceImpl extends ServiceImpl<HisShareLogMapper, HisShareLog> implements IHisShareLogService {

    @Autowired
    private InsShareLogService insShareLogService;

    @Scheduled(cron = "${meily.scheduled.default.cron}")
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public void migrateData() {
        AsyncManager.me().execute(new TimerTask() {
            @Override
            public void run() {
                List<InsShareLog> insLogList = insShareLogService.selectExpireData();
                if (!insLogList.isEmpty()) {
                    List<HisShareLog> hisLogList = CglibUtil.copyList(insLogList, HisShareLog::new);
                    saveBatch(hisLogList);
                    insShareLogService.removeBatchByIds(insLogList);
                }
            }
        });
    }
}

